package yhao.infra.feature.alipay.core;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import yhao.infra.feature.alipay.ConfigFactory;
import yhao.infra.feature.alipay.core.config.AliPayConfig;

/**
 * @author yoara
 * <p> Created on 2019/9/17 </p>
 */
public abstract class BaseAliPayRequest<T> {
    private String out_trade_no;//订单号

    @JSONField(serialize = false)
    public static final String SUCCESS_CODE = "10000";

    @JSONField(serialize = false)
    protected static Logger logger = LoggerFactory.getLogger(ConfigFactory.LOGGER_NAME);

    @JSONField(serialize = false)
    protected String bizContent;

    @JSONField(serialize = false)
    protected AlipayClient alipayClient;

    @JSONField(serialize = false)
    protected AliPayConfig alipayConfig;

    public final T doRequest(AliPayService aliPayService){
        bizContent = JSONObject.toJSONString(this);
        alipayClient = aliPayService.getAlipayClient();
        alipayConfig = aliPayService.getAlipayConfig();
        return doRequest();
    }

    protected abstract T doRequest();

    protected String logRequest(AlipayRequest aliPayRequest) {
        String method = new StringBuilder()
                .append("[").append(aliPayRequest.getApiMethodName()).append("]").toString();
        logger.info(method + bizContent);
        return method;
    }

    public BaseAliPayRequest(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }
}
